package Models;

import DB.DBCar;
import DB.DBEmployee;
import DB.DBRoute;
import DB.DBSchedule;
import DB.RowInterface;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
 
public class Schedule implements RowInterface
{
	private int scheduleNo;
	private int employeeNo;
	private Employee employee;
	private int carNo;
	private Car car;
	private int routeNo;
	private Route route;
	private Date date;

	public Schedule ()
	{
		this.employee = new Employee();
		this.route = new Route();
		this.car = new Car();
	}

	public void importData(ResultSet data) throws SQLException
	{
		this.setScheduleNo(data.getInt("scheduleNo"));
		this.setEmployeeNo(data.getInt("employeeNo"));
		this.setCarNo(data.getInt("carNo"));
		this.setRouteNo(data.getInt("routeNo"));
		this.setDate(data.getDate("date"));

		DBEmployee dbe = new DBEmployee();
		Employee employeeRow = dbe.fetchByNo(getEmployeeNo());
		if(employeeRow == null) employeeRow = new Employee();
		this.setEmployee(employeeRow);

		DBCar dbc = new DBCar();
		Car carRow = dbc.fetchByNo(getCarNo());
		if(carRow == null) carRow = new Car();
		this.setCar(carRow);
		
		DBRoute dbr = new DBRoute();
		Route routeRow = dbr.fetchByNo(getRouteNo());
		if(routeRow == null) routeRow = new Route();
		this.setRoute(routeRow);
	}
	
	public HashMap exportData()
	{
		HashMap data = new HashMap();
		data.put("employeeNo", getEmployeeNo());
		data.put("carNo", getCarNo());
		data.put("routeNo", getRouteNo());
		data.put("date", getDateToString(true));
		return data;
	}

	public void setData(String dateS, Employee employee, Route route, Car car) throws EmptyValueException, NumberFormatException
	{
		if(!dateS.equals("") && employee != null && route != null && car != null)
		{
			int day = Integer.parseInt(dateS.substring(0, 2));
			int month = Integer.parseInt(dateS.substring(3, 5))-1;
			int year = Integer.parseInt(dateS.substring(6, 10))-1900;
			
			Date dateTemp = new Date(year, month, day);
			this.setDate(dateTemp);

			this.setEmployee(employee);

			this.setRoute(route);

			this.setCar(car);
		}
		else
		{
			throw new EmptyValueException();
		}
	}

	public void refreshData()
	{
		Schedule schedule = new DBSchedule().fetchByNo(getScheduleNo());
		this.setDate(schedule.getDate());
		this.setEmployeeNo(schedule.getEmployeeNo());
		this.setCarNo(schedule.getCarNo());
		this.setRouteNo(schedule.getRouteNo());
	}

	public static String[] getSearchColumns()
	{
		return new String[] { "scheduleNo", "date", "employeeNo", "employeeName", "carNo", "carLicensePlate", "routeNo" };
	}

	public Car getCar()
	{
		return car;
	}

	public int getCarNo()
	{
		return carNo;
	}

	public Date getDate()
	{
		return date;
	}

	public String getDateToString(boolean toDB)
	{
		int day = date.getDate();
		String dayS = String.valueOf(day);
		int month = date.getMonth()+1;
		String monthS = String.valueOf(month);
		int year = date.getYear()+1900;
		String yearS = String.valueOf(year);

		if(dayS.length() == 1)
		{
			dayS = "0"+dayS;
		}
		if(monthS.length() == 1)
		{
			monthS = "0"+monthS;
		}

		if(toDB)
		{
			return yearS+"-"+monthS+"-"+dayS;
		}
		else
		{
			return dayS+"-"+monthS+"-"+yearS;
		}
	}

	public Employee getEmployee()
	{
		return employee;
	}

	public int getEmployeeNo()
	{
		return employeeNo;
	}

	public Route getRoute()
	{
		return route;
	}

	public int getRouteNo()
	{
		return routeNo;
	}

	public int getScheduleNo()
	{
		return scheduleNo;
	}

	public void setCar(Car car)
	{
		this.car = car;
		this.setCarNo(car.getCarNo());
	}

	public void setCarNo(int carNo)
	{
		this.carNo = carNo;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public void setEmployee(Employee employee)
	{
		this.employee = employee;
		this.setEmployeeNo(employee.getEmployeeNo());
	}

	public void setEmployeeNo(int employeeNo)
	{
		this.employeeNo = employeeNo;
	}

	public void setRoute(Route route)
	{
		this.route = route;
		this.setRouteNo(route.getRouteNo());
	}

	public void setRouteNo(int routeNo)
	{
		this.routeNo = routeNo;
	}

	public void setScheduleNo(int scheduleNo)
	{
		this.scheduleNo = scheduleNo;
	}
}